package gcontent.filter;

import gcontent.entities.Content;
import gcontent.entities.Site;
import gcontent.filter.error.FilterEx;

public class Filter {

	public static final String DEFAULT_TYPE = "*/*";
	
	/* filter status */
	public static final int STATUS_OK = 1;			/* filter successfully checked , not continue chain */
	public static final int STATUS_FAILED = 1 << 1;		/* filter checked failed, not continue chain */
	public static final int STATUS_NEXT = 1 << 10;		/* filter successfully checked , continue chain */
	public static final int STATUS_EXIT = 1 << 11;		/* filter successfully checked , continue chain */

	public static final int STATUS_SUCCESS = (STATUS_OK); 
	
	
	protected int order = -1;
	protected String type = null;
	protected int success = STATUS_NEXT;
	protected int fail = STATUS_EXIT;
	protected String input = "";
	protected boolean reversed = false;
	
	/* content type */
	public String getType() { return type == null? DEFAULT_TYPE : type; }
	public void setType(String type) { this.type = type; }
	
	/* order in a content type chain */
	public int getOrder() { return this.order; }
	public void setOrder(int order) { this.order = order; }
	
	/* order in a content type chain */
	public int getSuccess() { return this.success; }
	public void setSuccess(int success) { this.success = success; }
	
	public int getFail() { return this.fail; }
	public void setFail(int fail) { this.fail = fail; }
	
	/* set filter input parameters */
	public void setInput(String input) throws FilterEx { this.input = input; }
	public String getInput() { return this.input; }
	
	/* check content data */
	public int check(Content content) { return STATUS_FAILED; }
	
	/* check site */
	public int check(Site site) { return STATUS_FAILED; }
	
	public void setReversed(boolean reversed) { this.reversed = reversed; }
	public boolean isReversed() { return this.reversed; }
	
	public boolean checkIfFileIsNamedLikeFilter(String phrase, String word)
	{
		boolean ret = false;
		int idx = -1;
		String file_name = "";	
		phrase = phrase.toLowerCase();
		word = word.toLowerCase();
		
		if (word.equals("*"))
			ret = true;
		else {
			if (-1 == (idx = phrase.lastIndexOf("/"))) {
				idx = phrase.lastIndexOf("\\");
			}	
			
			if (idx > -1) {
				file_name = phrase.substring(idx);
			}			
			
			if (!file_name.contains(word)) {
				ret = true;				
			}
		}
		
		return ret;		
	}
}
